programming4us
           
 
 
Windows Phone

Writing Your First Phone Application - Adding Code (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/25/2010 3:30:28 PM
This first Windows Phone 7 application is not going to do much, but we should get started and make something happen with the phone. Since this is your first Windows Phone 7 application, let’s not pretend it is a desktop application but instead let’s show off some of the touch capabilities.

First off, if you look at the text of the XAML, you should see the first line of text shows the root element of the XAML to be a PhoneApplicationPage. This is the basic class from which each page you create will derive. The x:Class declaration is the name of the class that represents the class. If you open the code file, you will see this code was created for you.

HelloWorldPhone.MainPage"
...
You will want to open up the code file for the XAML file. You can do this by right-clicking the XAML page and picking “View Code” or simply hit F7 to open up the code file. The initial code file is pretty simple but you should see what the basics are. The namespace and class name match the x:Class definition we see in the XAML. This is how the two files are related to each other. If you change one, you will need to change the other. You should also note that the base class for the MainPage class is the same as the root element of the XAML. They are all related to each other.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace HelloWorldPhone
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
}
}

These two files (the .xaml and the code file) are very tied to each other. In fact you can see that if you find an element in the XAML that has a name, it will be available in the code file. If you switch back to the .xaml file, click on the TextBlock that you created in Blend. You will notice in the Property pane that it does not have a name (as shown in Figure 1).
Figure 1. Naming a XAML Element (NamingAXamlElement.bmp)


If you click where it says ““, you can enter a name. Name the TextBlock “theStatus”. If you then switch over to the code file, you will be able to use that name as a member of the class:

...
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();

theStatus.Text = "Hello from Code";
}
}
...

At this point if you run the application (hitting F5 works), you will see that this line of code is being executed as the theStatus TextBlock is changed to show the new text (as seen in Figure 2).

Figure 2. Running the Application (RunningOurApp.bmp)


There is an important fact you should derive from the fact that named elements in the XAML become part of the class: the job of the XAML is to build an object graph. The hierarchy of the XAML is just about creating the hierarchy of objects. At runtime, you can modify these objects in whatever way you want.

Working with Events

Since you are building phone application, let’s show how basic events work. You can wire up events just as easily using standard language (e.g. C#) semantics. For example, you could handle the MouseLeftButtonUp event on theStatus to run code with the text is tapped:

...
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();

theStatus.Text = "Hello from Code";

theStatus.MouseLeftButtonUp +=
new MouseButtonEventHandler(theStatus_MouseLeftButtonUp);
}

void theStatus_MouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
theStatus.Text = "Status was Tapped";
}
}
...

When you tap on theStatus, the MouseLeftButtonUp event will be fired (which is what causes the code in the event handler to be called. All events work in this simple fashion, but the number and type of events in Silverlight for the Windows Phone 7 varies widely.

Debugging In the Emulator

If clicking the user interface was not working the way we would like, it might help if we could stop the operation during an event to see what was happening during execution. We can do this by debugging our operation. We can use the debugger to set breakpoints and stop in code while using the emulator. Place the text cursor inside the event handler and press F9 to create a breakpoint. When you run the application (again, hit F5) you can see that when you click on theStatus TextBlock that the debugger stops inside the event handler. You can hover your mouse over specific code elements (e.g. theStatus.Text) to see the value in a popup (as shown in Figure 3).

Figure 3. Debugging with a Breakpoint (DebuggingBreakpoint.bmp)


Pressing the F5 key while stopped at a breakpoint will cause the application to continue running. There are other ways to walk through the code, but for now that should be sufficient to get you started. Using the emulator is the most common way you will develop your applciations, but there are times some interactions are difficult to do with the emulator (e.g. multi-touch, using phone sensors, etc.) that debugging directly on a device would be very useful. Luckily, debugging on the device is supported and works pretty simply.

Other -----------------
- Developing Applications for Windows Phone 7 : Designing with Blend
- Developing Applications for Windows Phone 7 : Creating a New Project
- Developing Applications for Windows Phone 7 with Silverlight : Preparing Your Machine
- Windows Phone 7 : Picking Ringtones and Alerts
- Windows Phone 7 : Changing Themes and Wallpaper
- Windows Phone 7 : Customizing the Start Screen
- Windows Phone 7 : Connecting to a Wi-Fi Hotspot
- Windows Phone 7 : Setting Up Facebook
- Windows Phone 7 : Setting Up E-Mail and Your Calendar
- Windows Phone 7 : Taking a Quick Tour (part 2)
- Windows Phone 7 : Taking a Quick Tour (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us